package com.example.demo.Server;

import com.example.demo.Dao.BookDao;
import com.example.demo.SpringBookDemoApplication;
import com.example.demo.enums.StatusEnum;
import com.example.demo.mapper.BookMapper;
import com.example.demo.model.BookInfo;
import com.example.demo.model.PageResult;
import com.example.demo.model.RequestPage;
import com.example.demo.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.imageio.plugins.bmp.BMPImageWriteParam;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
//@Component
@Service
public class BookServer {
//    @Autowired
//    BookDao bookDao;
BookDao bookDao = new BookDao();
    public List<BookInfo> bookServer(){
        List<BookInfo> bookInfos = bookDao.mockData();
        for(BookInfo bookInfo : bookInfos){
            if(bookInfo.getStatus() == 1){
                bookInfo.setStateCN("可借阅");
            }else{
                bookInfo.setStateCN("不可借阅");
            }
        }
        return bookInfos;
    }

    @Autowired
    private BookMapper bookMapper;
    public boolean addBook(BookInfo bookInfo) {
       return bookMapper.insert(bookInfo);

    }

    public PageResult<BookInfo> getListByPage(RequestPage requestPage) {
        Integer count = bookMapper.count();
        Integer offset  = requestPage.offset();
        List<BookInfo> bookInfos = bookMapper.getListByPage(offset,requestPage.getPageSize());
        for(BookInfo bookInfo : bookInfos){
            if(bookInfo.getStatus() == 1){
                bookInfo.setStateCN("可借阅");
            }else{
                bookInfo.setStateCN("不可借阅");
            }
//            StatusEnum.getNameByCode(bookInfo.getStatus());

        }
        return new PageResult<>(bookInfos,count,requestPage);
    }


    public BookInfo getBookInfo(Integer bookId) {
        return bookMapper.getBookInfo(bookId);

    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }

    public Integer deleteBook(Integer id) {
        return bookMapper.deleteBoook(id);
    }

    public Integer batchDelete(List<Integer> ids) {
        return bookMapper.batchDelete(ids);
    }
}
